#include "StdAfx.h"
#include "DToolRun.h"

DToolRun::DToolRun(void)
{
}

DToolRun::~DToolRun(void)
{
}

bool DToolRun::Init()
{

	DTexture* tmpTexture = new DTexture;
	tmpTexture->Load(L"../data/Snow.jpg");
	m_TextureManager->AddTexture(tmpTexture);

	m_pDevice->SetRenderState(	D3DRS_CULLMODE, D3DCULL_NONE );
	m_pDevice->SetRenderState(	D3DRS_LIGHTING, FALSE );	
	m_pDevice->SetRenderState(	D3DRS_FILLMODE, D3DFILL_SOLID );

	return true;
}

bool DToolRun::Frame()
{
	MoveSeletUI();
	return true;
}

bool DToolRun::Render()
{
	m_UIManager.StageRun(m_SeletUI.stageIndex);
	return true;
}

bool DToolRun::Release()
{
	return true;
}
void DToolRun::InsertUIStage(int index, UIStage* pUIStage)
{
	m_UIManager.InsertUIStage(index, pUIStage);
}

void DToolRun::InsertUIScene( UIScene* pSrc, UIStage* pDest )
{
	pDest->InsertUIScene(pSrc);
}

void DToolRun::InsertUIControl( UIControl* pSrc, UIStage* pDest )
{
	pSrc->SetDevice(m_pDevice);
	pDest->InsertUIControl(pSrc);
}

void DToolRun::InsertUIControl( UIControl* pSrc, UIScene* pDest )
{
	pSrc->SetDevice(m_pDevice);
	pDest->InsertUIControl(pSrc);
}
bool DToolRun::CheckCollision(RECT rect, POINT point)
{
	if(point.x < rect.left)
		return false;
	if(point.x > rect.right)
		return false;
	if(point.y < rect.top)
		return false;
	if(point.y > rect.bottom)
		return false;

	return true;
}
void DToolRun::MoveSeletUI()
{
	switch(m_SeletUI.itemType)
	{
	case TreeInf::IT_CTRL:
		{
			UIControl* pUICtrl = (UIControl*)m_SeletUI.pTreeItem;
			if( CheckCollision(pUICtrl->GetRect(), m_pInput->GetCursorPos()) )
			{
				if(m_pInput->IsKeyDownFirst(VK_LBUTTON))
				{
					pUICtrl->m_IsMouseDrag = true;
				}
				if(m_pInput->IsKeyUpFirst(VK_LBUTTON))
				{
					pUICtrl->m_IsMouseDrag = false;
				}		
			}
			if(pUICtrl->m_IsMouseDrag)
				pUICtrl->SetPos(pUICtrl->GetPosX()+m_pInput->GetCursorMoveX(), pUICtrl->GetPosY()+m_pInput->GetCursorMoveY());
		}
		break;
	default:
		break;
	}
}